﻿//Copyright (C) 2011  Matthew White

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

namespace SccmBoundaryHealth
{
    using System;
    using System.Collections;

    internal class ArgumentParser
    {
        public static void AddKeyValuePair(Hashtable argTable, string key, string value)
        {
            try
            {
                if (!argTable.ContainsKey(key))
                {
                    argTable.Add(key, value);
                }
                else
                {
                    argTable[key] = value;
                }
            }
            catch (ArgumentException exception)
            {
                Console.WriteLine(exception.Message + Environment.NewLine + exception.StackTrace);
            }
            catch (Exception exception2)
            {
                Console.WriteLine(exception2.Message + Environment.NewLine + exception2.StackTrace);
            }
        }

        public static Hashtable Parse(string[] args)
        {
            Hashtable argTable = new Hashtable();
            try
            {
                if ((args == null) || (args.Length == 0))
                {
                    argTable.Add("Invalid", "true");
                    return argTable;
                }
                foreach (string str2 in args)
                {
                    if ((str2.StartsWith("-") || str2.StartsWith("/")) && (str2.Contains("=") && !str2.Contains("?")))
                    {
                        string[] strArray = str2.Substring(1, str2.Length - 1).Split(new char[] { '=' });
                        string key = strArray[0].ToString();
                        string str = strArray[1].ToString();
                        if ((str == null) || (str.Trim() == ""))
                        {
                            argTable.Add("Invalid", "true");
                            return argTable;
                        }
                        AddKeyValuePair(argTable, key, str);
                    }
                    else if (str2.Contains("?"))
                    {
                        AddKeyValuePair(argTable, "?", "true");
                    }
                    else
                    {
                        AddKeyValuePair(argTable, "Invalid", "true");
                    }
                }
                return argTable;
            }
            catch (ArgumentOutOfRangeException exception)
            {
                Console.WriteLine("Arguments out of range, please check error log" + exception.Message + Environment.NewLine + exception.StackTrace);
                return argTable;
            }
            catch (Exception exception2)
            {
                Console.WriteLine(exception2.Message + Environment.NewLine + exception2.StackTrace);
                return argTable;
            }
        }
    }
}

